package com.hsytech.dijiebao.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsytech.dijiebao.MainActivity;
import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.account.SignInActivity;
import com.hsytech.dijiebao.base.BaseApplication;
import com.hsytech.dijiebao.base.BaseFragment;
import com.hsytech.dijiebao.bean.HomeHelpHotle;
import com.hsytech.dijiebao.bean.HomeHelpTaxi;
import com.hsytech.dijiebao.bean.HomeHotelBean;
import com.hsytech.dijiebao.bean.HomeMyHotle;
import com.hsytech.dijiebao.bean.HomeMyTaxi;
import com.hsytech.dijiebao.bean.HomeTaxiBean;
import com.hsytech.dijiebao.home.fragment.HotleFrament;
import com.hsytech.dijiebao.home.fragment.TaxiFrament;
import com.hsytech.dijiebao.person.activity.SeeCityActivity;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.HttpUtil;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.hsytech.dijiebao.view.CircleImageView;
import com.hsytech.dijiebao.view.PagerSlidingTabStrip;
import com.squareup.picasso.Picasso;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Frament00 extends BaseFragment {
    public static Boolean isVisvisble = false;
    private View root;
    private ViewPager activity_repair_viewpager;
    private HotleFrament hotleFrament;
    private TaxiFrament taxiFrament;
    private List<Fragment> myFragmentList = new ArrayList<Fragment>();

    private CircleImageView top_back_btn;
    private TextView bar_title_tv;
    private PagerSlidingTabStrip tabs;
    private MyPagerAdapter adapter;
    private DisplayMetrics dm;
    private int currentColor = 0xff66ccff;
    private int afterColor = 0xff66ccff;
    private RelativeLayout relativeLayout_city;
    private List<HomeHotelBean> hm;
    private List<HomeHotelBean> hh;
    private List<HomeTaxiBean> hh1;
    private List<HomeTaxiBean> hm1;
    private TextView new_city;
    private FragmentManager fm;
    private String head_photo;
    private String defaultCityName;

    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            try {
                JSONObject result = new JSONObject(response);
                hm = com.alibaba.fastjson.JSONArray.parseArray(
                        result.optJSONArray("have_hotel").toString(), HomeHotelBean.class);
                hh = com.alibaba.fastjson.JSONArray.parseArray(
                        result.optJSONArray("find_hotel").toString(), HomeHotelBean.class);
                hm1 = com.alibaba.fastjson.JSONArray
                        .parseArray(result.optJSONArray("have_car").toString(), HomeTaxiBean.class);
                hh1 = com.alibaba.fastjson.JSONArray.parseArray(
                        result.optJSONArray("find_car").toString(), HomeTaxiBean.class);
                JSONObject def_city = result.optJSONObject("def_city");
                head_photo = result.optString("head_photo");
                defaultCityName = def_city.optString("name");
                Log.d("mylog", "defaultCityName:::" + defaultCityName);
                String cityName = SharedPreferencesTools.getCityName(getActivity());
                if (!TextUtils.isEmpty(cityName)) {
                    new_city.setText(cityName);
                } else {
                    new_city.setText(defaultCityName);
                }
                Picasso.with(getActivity()).load("http://djb.dev.hsydev.com/Upload/image_sm/" + head_photo).fit()
                        .into(top_back_btn);
                hotleFrament.setData(hm, hh);
                taxiFrament.setData(hh1, hm1);
                adapter.notifyDataSetChanged();


            } catch (JSONException e) {
                e.printStackTrace();
            }
            BaseApplication.MyDialog.dismiss();
//
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            BaseApplication.MyDialog.dismiss();
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        getHotleHttp();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_home_main, container, false);
        dm = getResources().getDisplayMetrics();
        Log.d("mylog", "onCreateView");
        initBody();
        return root;
    }

    private void getHotleHttp() {
        BaseApplication.MyDialog.show(getActivity());
        HttpUtil.httpGet(getActivity(), 0, Constants.HOME + "?city_ids=" + SharedPreferencesTools.getCityID(getActivity()), callBack);

    }

    private void initTitle() {
        new_city = (TextView) root.findViewById(R.id.new_city);
        top_back_btn = (CircleImageView) root.findViewById(R.id.top_back_btn);
        relativeLayout_city = (RelativeLayout) root.findViewById(R.id.relativeLayout_city);
        bar_title_tv = (TextView) root.findViewById(R.id.bar_title_tv);
        top_back_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (TextUtils.isEmpty(SharedPreferencesTools.getTOKEN(getActivity()))) {
                    Toast.makeText(getActivity(), R.string.msg_no_have_token, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    startActivity(intent);

                } else {
                    MainActivity.mFragmentTabHost.setCurrentTab(3);
                }

            }
        });


        relativeLayout_city.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity(), SeeCityActivity.class);
                startActivity(intent);
            }
        });


        bar_title_tv.setText("地接宝");
    }

    private void setTabsValue() {

        // ����Tab���Զ��������Ļ��
        tabs.setShouldExpand(true);

        // ����Tab�ķָ�����͸����
        tabs.setDividerColor(Color.TRANSPARENT);
        // ����Tab�ײ��ߵĸ߶�
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // ����Tab Indicator�ĸ߶�
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, dm));
        // ����Tab�������ֵĴ�С
        tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, dm));
        // ����Tab Indicator����ɫ
        tabs.setIndicatorColor(currentColor);
        // ����ѡ��Tab���ֵ���ɫ (�������Զ����һ������)
        tabs.setSelectedTextColor(afterColor);
        // ȡ�����Tabʱ�ı���ɫ
        tabs.setTabBackground(0);

        tabs.setTextColor(getResources().getColor(R.color.gary_first));

    }

    private void initBody() {

        activity_repair_viewpager = (ViewPager) root.findViewById(R.id.activity_repair_viewpager);

        tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);

        setTabsValue();

        hotleFrament = new HotleFrament();
        taxiFrament = new TaxiFrament();

        myFragmentList.add(hotleFrament);
        myFragmentList.add(taxiFrament);

        adapter = new MyPagerAdapter(getChildFragmentManager(), myFragmentList);
        activity_repair_viewpager.setAdapter(adapter);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
                getResources().getDisplayMetrics());
        activity_repair_viewpager.setPageMargin(pageMargin);
        tabs.setViewPager(activity_repair_viewpager);
        initTitle();
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"酒店", "租车"};
        private List<Fragment> myFragmentsList;
        private FragmentManager myFragmentManager;

        public MyPagerAdapter(FragmentManager fragmentmanager, List<Fragment> list) {
            super(fragmentmanager);
            myFragmentsList = list;
            this.myFragmentManager = fragmentmanager;
        }

        public int getItemPosition(Object obj) {
            // return super.getItemPosition(obj);
            return FragmentPagerAdapter.POSITION_NONE;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public int getCount() {
            return TITLES.length;
        }

        public Fragment getItem(int i) {
            return (Fragment) myFragmentsList.get(i);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }


    }
}
